package com.my.lock.v3;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by ZhongGang
 * at 2018/7/27 14:59
 */
public class CompareAndSwitch2 {
    private static final Logger LOGGER = LoggerFactory.getLogger(CompareAndSwitch2.class);

    public static void main(String[] args) throws Exception {
        AtomicBoolean locked = new AtomicBoolean(false);

        for (int i = 0; i < 1000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (locked.compareAndSet(false, true)) {
                        LOGGER.info("success to cas, locked: {}", locked.get());
                    } else {
                        LOGGER.info("fail to cas");
                    }
                }
            }).start();
        }
    }
}
